{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "312e190b-bd2b-480d-a951-6ca2f1970d10",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import os\n",
    "import cv2\n",
    "import PIL\n",
    "import io"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "398958f4-da71-49a8-9ea2-8f1b0923b14a",
   "metadata": {},
   "outputs": [],
   "source": [
    "data_dir = \"./48kpart2\"\n",
    "Categories = [ \"Inner\",\"Outer\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bb527af6-4940-4e87-af95-bf0d4dcab6d4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pathlib\n",
    "data_dir = pathlib.Path(data_dir)\n",
    "data_dir"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8fc8b72d-093e-490f-9f9e-471684747f63",
   "metadata": {},
   "outputs": [],
   "source": [
    "list(data_dir.glob('*/*.png'))[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e9540610-c1f4-4d83-b557-38ad3211b76c",
   "metadata": {},
   "outputs": [],
   "source": [
    "image_count = len(list(data_dir.glob('*/*.png')))\n",
    "print(image_count)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1d8b8b96-3f80-41b6-aba2-6c026182dc8e",
   "metadata": {},
   "outputs": [],
   "source": [
    "Inner = list(data_dir.glob('Inner/*'))\n",
    "Inner[:5]\n",
    "Outer = list(data_dir.glob('Outer/*'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "41f2933a-9507-4116-952a-6585458628e4",
   "metadata": {},
   "outputs": [],
   "source": [
    "PIL.Image.open(str(Inner[6]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10ba3ba8-3e0e-409d-bb09-337083b72f05",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "PIL.Image.open(str(Outer[9]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ff391599-3f61-4dbc-a30f-a5ea13a55cb2",
   "metadata": {},
   "outputs": [],
   "source": [
    "category_dict={\"Inner\":0,\"Outer\":1}\n",
    "images_dict = {\"Inner\" : Inner,\"Outer\" : Outer}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4ee8fe66-d77c-47b3-849a-e22d7ab36472",
   "metadata": {},
   "outputs": [],
   "source": [
    "img = cv2.imread(str(images_dict[\"Outer\"][1]))\n",
    "img.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "db8d25e6-dd86-4336-8065-2b8c1665871c",
   "metadata": {},
   "outputs": [],
   "source": [
    "X, y = [], []\n",
    "\n",
    "for category, images in images_dict.items():\n",
    "    for image in images:\n",
    "        img  = cv2.imread(str(image),0)\n",
    "#         resized_img = cv2.resize(img,(180,180))\n",
    "        X.append(img)\n",
    "        y.append(category_dict[category])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "35cb8220-1e0d-47f6-b3bd-c7e239ea6bd4",
   "metadata": {},
   "outputs": [],
   "source": [
    "X = np.array(X)\n",
    "y = np.array(y)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c6a533db-944e-418c-a507-261169f4a93a",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9f345207",
   "metadata": {},
   "outputs": [],
   "source": [
    "len(X_train)\n",
    "from PIL import Image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d6b68d65",
   "metadata": {},
   "outputs": [],
   "source": [
    "img = Image.fromarray(X_train[0])\n",
    "img.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7ee6f379-de3b-4c64-9a09-f0583f971832",
   "metadata": {},
   "outputs": [],
   "source": [
    "# X_train_scaled = (X_train) / 255\n",
    "# X_test_scaled = (X_test) / 255\n",
    "# for n in range(len(X_train)):\n",
    "#    X_train[n] = X_train[n]/255\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0b9a7888-fab5-49d4-ac1c-ebbda20268a5",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "# from tensorflow import keras \n",
    "import numpy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "66051efa-9c21-4d73-8f94-78247c53205c",
   "metadata": {},
   "outputs": [],
   "source": [
    "from tensorflow.keras import layers\n",
    "from tensorflow.keras.models import Sequential\n",
    "from tensorflow.keras.layers import (\n",
    "    Dense,\n",
    "    Conv2D,\n",
    "    MaxPool2D,\n",
    "    Flatten,\n",
    "    Dropout,\n",
    "    BatchNormalization,\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7c362db1",
   "metadata": {},
   "outputs": [],
   "source": [
    "num_classes = 2\n",
    "model = Sequential([\n",
    "  layers.Conv2D(16, 3, padding='same', activation='relu',input_shape=(180,180,3)),\n",
    "  layers.MaxPooling2D(),\n",
    "  layers.Conv2D(32, 3, padding='same', activation='relu'),\n",
    "  layers.MaxPooling2D(),\n",
    "  layers.Conv2D(64, 3, padding='same', activation='relu'),\n",
    "  layers.MaxPooling2D(),\n",
    "  layers.Flatten(),\n",
    "  layers.Dense(128, activation='relu'),\n",
    "  layers.Dense(1,activation=\"sigmoid\")\n",
    "])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "120d9c1e",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d47e4452",
   "metadata": {},
   "outputs": [],
   "source": [
    "model.fit(X_train, y_train,steps_per_epoch=33,epochs=5, validation_data=(X_test, y_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5a39e0bf",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "model.evaluate(X_test,y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1e88bd10",
   "metadata": {},
   "outputs": [],
   "source": [
    "model.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "626177f0",
   "metadata": {},
   "outputs": [],
   "source": [
    "def predictImage(filename):\n",
    "    img1 = image.load_img(filename,target_size=(180,180))\n",
    "    \n",
    "    plt.imshow(img1)\n",
    " \n",
    "    Y = image.img_to_array(img1)\n",
    "    \n",
    "    X = np.expand_dims(Y,axis=0)\n",
    "    val = model.predict(X)\n",
    "    print(val)\n",
    "    if val == 1:\n",
    "        \n",
    "        plt.xlabel(\"F\",fontsize=30)\n",
    "        \n",
    "    \n",
    "    elif val == 0:\n",
    "        \n",
    "        plt.xlabel(\"H\",fontsize=30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bfd5aaf8",
   "metadata": {},
   "outputs": [],
   "source": [
    "from tensorflow.keras.preprocessing import image\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8d31f9de",
   "metadata": {},
   "outputs": [],
   "source": [
    "predictImage(r\"D:/User/Downloads/Classification/Classification/Inner/Normal_0_110.png \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e0109c7d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "LD_Env",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
